package com.chinaoryx;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: hyman.zhang
 * Date: 12-1-27
 * Time: 下午8:54
 * To change this template use File | Settings | File Templates.
 */
public class TaskManagerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String taskId = request.getParameter("taskId");
        String operate = request.getParameter("operate");

        TaskObject taskObject = ThreadFactory.newInstance().getTaskObject(taskId);

        if ("start".equals(operate)) {
            if (taskObject != null) {
//                MyThread myThread = new MyThread(taskObject.getTaskId(), taskObject.getInterval() * 1000);
//                taskObject.setMyThread(myThread);

                taskObject.getThread().start();
            }
        }

        if ("wait".equals(operate)) {
            if (taskObject != null) {

                    taskObject.getMyThread().setWaitFlag(true);
//                taskObject.getMyThread().ABC = true;

            }
        }

        if ("notify".equals(operate)) {
            if (taskObject != null) {
                synchronized (taskObject.getMyThread()) {
                    taskObject.getMyThread().notifyAll();
                }
            }
        }

        if ("interrupt".equals(operate)) {
            if (taskObject != null) {
                taskObject.getThread().interrupt();
            }
        }


        response.sendRedirect("TaskListServlet");
    }
}
